<?php


function runSQL($rsql) {
include('connection.php');
	
	
	
	$result = mysql_query($rsql) or die ($rsql);
	return $result;
	mysql_close($con);
}
function get_type($str)
{
	switch($str)
	{
		case "1":
		return "Airport";
		break;
		case "2":
		return "State";
		break;
		case "3":
		return "Country";
		break;
		case "4":
		return "Region";
		break;
 		case "5":
		return "Any Where";
		break;


	}
}
function get_status($str)
{
	switch($str)
	{
		case "1":
		return "<span style='color:green;'>Active</span>";
		break;
		case "0":
		return "<span style='color:red;'>Paused</span>";
		break;

	}
}
function get_name($type,$code)
{
	switch($type)
	{
		case "1":
		$query = mysql_query("SELECT `name` from `airport_code` WHERE airport_code='$code' order by rank") ;
		$row=mysql_fetch_array($query);
		return $row[0];
		break;
		case "2":
		$query = mysql_query("SELECT `state_name` from `states` WHERE state_code='$code'") ;
		$row=mysql_fetch_array($query);
		return $row[0];
		break;
		case "3":
		$query = mysql_query("SELECT `country_name` from `countries` WHERE country_code='$code'") ;
		$row=mysql_fetch_array($query);
		return $row[0];
		break;
		case "4":
		$query = mysql_query("SELECT `region_name` from `region` WHERE region_code='$code'") ;
		$row=mysql_fetch_array($query);
		return $row[0];
		break;
		case "5":
		return "ALL";
		break;
		

	}
	
}
function get_req($config)
{
	$qe = mysql_query("SELECT * FROM requests where config_id='$config'");
	$num=mysql_num_rows($qe);
	return $num;
}
function countRec($fname,$tname) {
	$sql = "SELECT count($fname) FROM $tname ";
	$result = runSQL($sql);
	while ($row = mysql_fetch_array($result)) {
		return $row[0];
	}	
}
$page = $_POST['page'];
$rp = $_POST['rp'];
$sortname = $_POST['sortname'];
$sortorder = $_POST['sortorder'];

if (!$sortname) $sortname = 'name';
if (!$sortorder) $sortorder = 'desc';

$sort = "ORDER BY $sortname $sortorder";

if (!$page) $page = 1;
if (!$rp) $rp = 10;

$start = (($page-1) * $rp);

$limit = "LIMIT $start, $rp";

$query = $_POST['query'];
$qtype = $_POST['qtype'];

$where = "";
if ($query) $where = " WHERE $qtype LIKE '%$query%' ";

$sql = "SELECT idConfiguration,dep_code,arr_code,dep_type,arr_type,status,timestamp FROM configuration $where $sort $limit";
$result = runSQL($sql);

$total = countRec("idConfiguration","configuration $where");

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" ); 
header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" ); 
header("Cache-Control: no-cache, must-revalidate" ); 
header("Pragma: no-cache" );
header("Content-type: text/x-json");
$json = "";
$json .= "{\n";
$json .= "page: $page,\n";
$json .= "total: $total,\n";
$json .= "rows: [";
$rc = false;
$i=0;
while ($row = mysql_fetch_array($result)) {
	$i++;
	if ($rc) $json .= ",";
	$json .= "\n{";
	$json .= "id:'".$i."',";
	$json .= "cell:['".$i."'";
	$json .= ",'".addslashes(get_name($row['dep_type'],$row['dep_code'])." (".get_type($row['dep_type'])).")'";
	$json .= ",'".addslashes(get_name($row['arr_type'],$row['arr_code'])." (".get_type($row['arr_type'])).")'";
	$json .= ",'".addslashes(get_status($row['status']))."'";
	$arr= explode("-",$row["timestamp"]);
    $json .= ",'".addslashes(date('d-M-y',mktime(0,0,0,intval($arr[1]),intval(substr($arr[2],0,2)),intval($arr[0]))))."'";
	$json .= ",'".addslashes("<input type='button' name='show' id='Show' value='".get_req($row['idConfiguration'])." REQ'  onclick='show_request(&#39;".$row['idConfiguration']."&#39;)' />")."'";
	$json .= ",'".addslashes("<a href='#' onclick='show_markup(&#39;".$row['idConfiguration']."&#39;)'><img src='css/money.png' align='absmiddle'  border='0'></a>")."'";
	$json .= ",'".addslashes("<a href='#' onclick='edit_config(&#39;".$row['idConfiguration']."&#39;)'><img src='css/application_edit.png' align='absmiddle'  border='0'></a>")."'";
	$json .= ",'".addslashes("<a href='#' onclick='delete_config(&#39;".$row['idConfiguration']."&#39;)'><img src='css/application_delete.png' align='absmiddle' border='0'></a>")."']";
	$json .= "}";
	$rc = true;		
}
$json .= "]\n";
$json .= "}";
echo $json;
?>